//
//  TableSearchController.m
//  ProjectFrame
//
//  Created by RubyJobs on 2018/11/9.
//  Copyright © 2018 个人. All rights reserved.
//

#import "TableSearchController.h"

@interface TableSearchController ()<UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource>

@end

@implementation TableSearchController
- (void)viewWillAppear:(BOOL)animated{
    self.searchVC.active = NO;
    [self.mainTableView reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpTable];
}
- (void)setUpTable{
    UITableView *mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH , SCREEN_HEIGHT - FIT64IPHONEX) style:UITableViewStylePlain];
    //签订代理
    mainTableView.delegate = self;
    mainTableView.dataSource = self;
    //去掉多余分割线
    mainTableView.tableFooterView = [UIView new];
    
    //分割线距离边框位置
    [mainTableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    
    //关闭系统偏移量
    if (@available(iOS 11.0, *)) {
        [mainTableView setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
    } else {
        //iOS8.0
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.definesPresentationContext = YES;
    //搜索栏
    self.searchVC = [[UISearchController alloc]initWithSearchResultsController:nil];
    //搜索栏代理
    self.searchVC.searchBar.delegate = self;
    self.searchVC.dimsBackgroundDuringPresentation = NO;
    //    self.searchVC.searchBar.backgroundColor = [UIColor blackColor];
    //包着搜索框外层的颜色
    self.searchVC.searchBar.barTintColor = [UIColor colorWithHexString:@"#f2f2f2"];
    self.searchVC.searchBar.placeholder= @"搜索";
    self.searchVC.searchBar.frame = CGRectMake(self.searchVC.searchBar.frame.origin.x, self.searchVC.searchBar.frame.origin.y, self.searchVC.searchBar.frame.size.width, 56.0);
    //隐藏搜索框上下2条黑线
    UIImageView *barImageView = [[[self.searchVC.searchBar.subviews firstObject] subviews] firstObject];
    //二进制颜色
    barImageView.layer.borderColor = [UIColor colorWithHexString:@"#fbfbfb"].CGColor;
    barImageView.layer.borderWidth = 1;
    mainTableView.tableHeaderView = self.searchVC.searchBar;
    [self.view addSubview:mainTableView];
    self.mainTableView = mainTableView;
}
- (void)setTableList:(NSMutableArray *)tableList{
    _tableList = tableList;
    [self.mainTableView reloadData];
}
#pragma mark-代理相关
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.searchVC.active) {
        return _searchArr.count;//返回单元格个数
    }else{
        return _tableList.count;//返回单元格个数
    }
    
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    return 45;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if (self.searchVC.active) {
        self.searchVC.searchBar.text = @"";
        if (self.searchArr.count == 0) {
            return;
        }
    }else{
        
    }

    
    
}
//table重用池xib版
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Identifier"];
    }
    return cell;
    
}

#pragma mark - searchDelegate
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    if (searchBar.text.length == 0) {
        return;
    }
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    self.searchVC.active = NO;
    [self.mainTableView reloadData];
}
//把搜索改为中文搜索
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    self.searchVC.searchBar.showsCancelButton = YES;
    for(id cc in [self.searchVC.searchBar.subviews[0] subviews]){
        if([cc isKindOfClass:[UIButton class]]){
            UIButton *btn = (UIButton *)cc;
            [btn setTitle:@"取消" forState:UIControlStateNormal];
        }
    }
}
- (void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBar.translucent = YES;
    [self.searchVC dismissViewControllerAnimated:YES completion:nil];
}
@end
